<?php

namespace App\Console\Commands\Tools\Menus;

use App\Kuafu\Category\Model\CategoryModel;
use App\Kuafu\Product\Model\Product;
use App\Kuafu\ProductMenu\Model\ProductMenu;
use App\Kuafu\ProductMenu\Service\ProductMenuService;
use Illuminate\Console\Command;

class ExportMenusCommand extends Command
{

    protected $signature = 'tools:export-menus';


    protected $description = '导出门店菜单';

    public function handle()
    {
        $productMenuIdArr = [
            606,
        ];

        foreach ($productMenuIdArr as $productMenuId) {

            ProductMenuService::menuExportById($productMenuId);die;
            $menuInfo = ProductMenu::query()->where('id', $productMenuId)->first();


            $menuProducts = Product::getMenuProducts($productMenuId)->keyBy('id');

            // 当前菜单下的所有category
            $pIdCategorys = CategoryModel::query()
                                         ->where('menuId', $productMenuId)
                                         ->select('id', 'pid', 'name')
                                         ->get()
                                         ->keyBy('id');

            $csvTitle = '菜品名称,菜品分类,销售价格,单位';

            foreach ($menuProducts as $product) {
                $categoryName = $pIdCategorys[$product['categoryId']]['name'] ?? '';

                $csvTitle .= vsprintf("\n%s,%s,%s,%s", [
                    $product['name'],
                    $categoryName,
                    bcdiv100($product['price']),
                    $product['unit'],
                ]);
            }

            $filename = str_replace(['【', '】', '/'], [''], $menuInfo->name);

            file_put_contents('./' . $filename . '.csv', $csvTitle);
        }
    }

}
